package com.tfjybj.itoo.teach.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.tenancy.TenancyContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.AdmTeachClassEntity;
import com.tfjybj.itoo.teach.entity.TeachClassEntity;
import com.tfjybj.itoo.teach.entity.TeacherTeachClassEntity;
import com.tfjybj.itoo.teach.entity.TrainingTeachClassEntity;
import com.tfjybj.itoo.teach.model.ArrangeCourseModel;
import com.tfjybj.itoo.teach.provider.dao.AdmTeachClassDao;
import com.tfjybj.itoo.teach.provider.dao.TeachClassDao;
import com.tfjybj.itoo.teach.provider.dao.TeacherTeachClassDao;
import com.tfjybj.itoo.teach.provider.dao.TrainingTeachClassDao;
import com.tfjybj.itoo.teach.provider.service.AdmTeachClassService;
import com.tfjybj.itoo.teach.provider.service.TeachClassService;
import com.tfjybj.itoo.teach.provider.service.TeacherTeachClassService;
import com.tfjybj.itoo.teach.provider.service.TrainingTeachClassService;
import com.tfjybj.itoo.teach.provider.until.RandomNumUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TrainingTeachClassService接口实现类
 * ${base}表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Service("trainingTeachClassService")
public class TrainingTeachClassServiceImpl extends BaseServicePlusImpl<TrainingTeachClassDao, TrainingTeachClassEntity> implements TrainingTeachClassService {
	
	//region 模板生成
    @Resource
    private TrainingTeachClassDao trainingTeachClassDao;

    @Resource
    private TeachClassDao teachClassDao;

    @Resource
    private TeachClassService teachClassService;
    @Resource
    private TeacherTeachClassDao teacherTeachclassDao;

    @Resource
    private TeacherTeachClassService teacherTeachclassService;
    @Resource
    private AdmTeachClassDao admTeachClassDao;
    @Resource
    private AdmTeachClassService admTeachClassService;
	
	 @Override
    public PageInfo<TrainingTeachClassEntity> queryByLikeTrainingProgramsId(String trainingProgramsId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(trainingTeachClassDao.queryLikeTrainingProgramsId(trainingProgramsId));
    }
	//endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 排课接口--添加上课班+上课班与行政班关系+上课班与培养计划关系 -- 胡泽宽
     *
     * @param arrangeCourseModel 排课实体
     * @return 上课班id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertTeachClass(ArrangeCourseModel arrangeCourseModel) {
        boolean result;
        Map<String,Map<String,String>> map=new HashMap<>();
        //1.添加上课班到tt_teach_class
        String operator= TenancyContext.UserID.get();
        TeachClassEntity teachClassEntity=new TeachClassEntity();
        teachClassEntity.setId(IdWorker.getIdStr());
        teachClassEntity.setOperator(operator);
        String grade="";
        String courseName=arrangeCourseModel.getCourseName();
        //按规矩生成上课班代码
        String teachClassName=this.findTeachClassName(grade,courseName);

        //获得随机六位数为上课班代码
        String code= RandomNumUtils.getCard();
        //查询是否有重复的code
        Integer i=trainingTeachClassDao.findByCode(code);

        if(i<=0){
            teachClassEntity.setCode(code);//上课班id
            teachClassEntity.setName(teachClassName);//上课班名称
            teachClassEntity.setCourseId(arrangeCourseModel.getCourseId());//课程id
            teachClassEntity.setCapacity(arrangeCourseModel.getCapacity());
            teachClassEntity.setSchoolYear(arrangeCourseModel.getSemesterId());
            //是否来自签到系统0：否1：是
            teachClassEntity.setIsSign(0);
            result=teachClassService.save(teachClassEntity);
            if(result){
                //2.添加教师和上课班关系

                TeacherTeachClassEntity distinctList=new TeacherTeachClassEntity();
                distinctList.setId(IdWorker.getIdStr());
                distinctList.setTeacherId(arrangeCourseModel.getTeacherId());
                distinctList.setTeachClassId(teachClassEntity.getId());
                distinctList.setSchoolYear(arrangeCourseModel.getSemesterId());
                result=teacherTeachclassService.save(distinctList);
            }
            if(result){
                //3.添加到行政班上课班关系表,获得所有的班级id
                List<String> classIds=arrangeCourseModel.getClassIds();
                List<AdmTeachClassEntity> list=new ArrayList<>();
                if(!CollectionUtils.isEmpty(classIds)){
                    for(String classId: classIds){
                        AdmTeachClassEntity admTeachClassEntity=new AdmTeachClassEntity();
                        admTeachClassEntity.setId(IdWorker.getIdStr());
                        admTeachClassEntity.setOperator(operator);
                        admTeachClassEntity.setTeachClassId(teachClassEntity.getId());
                        admTeachClassEntity.setClassId(classId);
                        list.add(admTeachClassEntity);
                    }
                    result = admTeachClassService.saveOrUpdateBatch(list);
                }
            }
//            if(result>0){
//                //4.添加到上课班培养计划关系表
//                //插入的参数
//                TrainingTeachClassEntity trainingTeachClass=new TrainingTeachClassEntity();
//                List<TrainingTeachClassEntity> trainingTeachClassEntity=new ArrayList<>();
//                String trainingProgramsId=arrangeCourseModel.getTrainingProgramsId();
//
//                    trainingTeachClass.setId(IdWorker.getIdStr()());
//                    trainingTeachClass.setOperator(operator);
//                    trainingTeachClass.setTeachClassId(teachClassEntity.getId());
//                    trainingTeachClass.setTrainingProgramsId(trainingProgramsId);
//                    trainingTeachClassEntity.add(trainingTeachClass);
//
//                trainingTeachClassDao.insertAll(trainingTeachClassEntity);
//            }
            if(result){
                return teachClassEntity.getId();
            }
        }
        return null;
    }

    /**
     * 依据策略生成上课班名称  2018年11月18日
     *
     * @param grade      年级
     * @param courseName 课程名称
     * @return 上课班名称
     * @author 胡泽宽
     */
    @Override
    public String findTeachClassName(String grade,String courseName ) {
        //策略使用生成上课班名称
        String teachClassName;
        if(StringUtils.isEmpty(grade)){
            teachClassName=courseName;
        }else {
            teachClassName=grade+courseName;
        }
        long teachClassNameCount=trainingTeachClassDao.selectCount(teachClassName);
        long count=teachClassNameCount+1;
        return teachClassName+count+"班";
    }

    /**
     * 删除排课接口--添加上课班+上课班与行政班关系+上课班与培养计划关系 -- 胡泽宽
     *
     * @param teachclassid 上课班id
     * @return 最后成功的个数result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteTeachClass(String teachclassid){
        //1.删除行政班上课班关系表
        int result;
        String operator= TenancyContext.UserID.get();
        result=admTeachClassDao.deleteByteachClassId(teachclassid,operator);
        if(result>0){
            //2.删除教师和上课班关系
          result= teacherTeachclassDao.deleteByteachClassId(teachclassid,operator);
          if(result>0){
              //3.删除上课班到tt_teach_class
             result= teachClassDao.deleteById(teachclassid);
          }
          if(result>0){
              return result;
          }
        }
        return null;
    }

}
